//
//  EditNotes.m
//  Bespoke
//
//  Created by Cheng on 12/10/15.
//
//

#import "EditNotes.h"
#import "DetailViewController.h"
#import "RequestAPIFunction.h"

@implementation EditNotes
@synthesize Bt_Edit, Bt_Done, TVw_Content;
@synthesize AxisX, AxisY, MLM_Detail, Note_Index, MLM_Detail_InBook, BK_En_object;
@synthesize delegate;

#define KEYBOARDHEIGHT 350
#define Picture_Tag 10000
#define ADD_NOTE_TAG -1
#define NOTES_API @"http://54.235.166.43/BeSpokeAPI/Notes/Upload.php"

- (void)CancelPressed:(id)sender {
    
    [Bt_Edit setTitle:@"E D I T" forState:UIControlStateNormal];
    [Bt_Edit removeTarget:self action:@selector(CancelPressed:) forControlEvents:UIControlEventTouchUpInside];
    [Bt_Edit addTarget:self action:@selector(EditButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [Bt_Done removeTarget:self action:@selector(DoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [Bt_Done addTarget:self action:@selector(DoneButtonPressed_Cancel:) forControlEvents:UIControlEventTouchUpInside];
    [self.delegate CancelPressed_EditNotes];
}

- (IBAction)DoneButtonPressed_Cancel:(id)sender {
        [Bt_Edit setTitle:@"E D I T" forState:UIControlStateNormal];
        [Bt_Edit addTarget:self action:@selector(EditButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.delegate CancelPressed_EditNotes];
}

- (IBAction)EditButtonPressed:(id)sender {
    
    [Bt_Edit setTitle:@"C A N C E L" forState:UIControlStateNormal];
    [Bt_Edit addTarget:self action:@selector(CancelPressed:) forControlEvents:UIControlEventTouchUpInside];
    TVw_Content.delegate = self;
    [TVw_Content setEditable:YES];
    [TVw_Content becomeFirstResponder];
    [Bt_Done addTarget:self action:@selector(DoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)DoneButtonPressed:(id)sender {
    
    self.center = CGPointMake(((DetailViewController *)self.delegate).view.bounds.size.width/2, ((DetailViewController *)self.delegate).view.bounds.size.height/2); 
    
    [self.TVw_Content resignFirstResponder];
    
    NSString *Note_XML;
    
    if (Note_Index==ADD_NOTE_TAG)
        Note_XML = [NSString stringWithFormat:@"<Note Id=\"\" Text=\"%@\" MediaId=\"%d\" AxisX=\"%d\" AxisY=\"%d\" User=\"%@\" />",
                    TVw_Content.text,
                    MLM_Detail.MediaID,
                    AxisX,
                    AxisY,
                    BK_En_object.userName];
    else
        Note_XML = [NSString stringWithFormat:@"<Note Id=\"%d\" Text=\"%@\" MediaId=\"%d\" AxisX=\"%d\" AxisY=\"%d\" User=\"%@\" />",
                    ((MediaListsNote *)[MLM_Detail.Array_Notes objectAtIndex:Note_Index]).Note_Id,
                    TVw_Content.text,
                    MLM_Detail.MediaID,
                    AxisX,
                    AxisY,
                    BK_En_object.userName];
    

    NSData *responseData =
    [RequestAPIFunction XML_PostRequest:Note_XML urlString:NOTES_API];
    
    //No Network Connection or time out.
    if (responseData == nil)
        return;
    
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData options:0 error:&error];
    
    //Check to upload is success.
    if ([[doc nodesForXPath:@"//Success" error:&error] count]!=0) {
        
        
        [Bt_Edit setTitle:@"E D I T" forState:UIControlStateNormal];
        [Bt_Edit removeTarget:self action:@selector(CancelPressed:) forControlEvents:UIControlEventTouchUpInside];
        [Bt_Edit addTarget:self action:@selector(EditButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [Bt_Done removeTarget:self action:@selector(DoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [Bt_Done addTarget:self action:@selector(DoneButtonPressed_Cancel:) forControlEvents:UIControlEventTouchUpInside];
        
        
        if (Note_Index==ADD_NOTE_TAG) {
            
            int NoteId = [[[[[doc nodesForXPath:@"//Success" error:nil] objectAtIndex:0] attributeForName:@"NoteId"] stringValue] integerValue];
            
            //Init
            MediaListsNote *MLN_Note = [[MediaListsNote alloc] init:NoteId
                                                        SetNoteText:TVw_Content.text
                                                           SetAxisX:AxisX
                                                           SetAxisY:AxisY];
            
            [MLM_Detail.Array_Notes addObject:MLN_Note];
            
            if (MLM_Detail_InBook!=nil)
                [MLM_Detail_InBook.Array_Notes addObject:MLN_Note];
            
            [self.delegate SetAxisX:AxisX SetAxisY:AxisY SetIndex:MLM_Detail.Array_Notes.count-1];
        }
        else {
            ((MediaListsNote *)[MLM_Detail.Array_Notes objectAtIndex:Note_Index]).NS_NoteText = TVw_Content.text;
            
            if (MLM_Detail_InBook!=nil)
                for (MediaListsNote *NoteOB in MLM_Detail_InBook.Array_Notes)
                    if (((MediaListsNote *)[MLM_Detail.Array_Notes objectAtIndex:Note_Index]).Note_Id==NoteOB.Note_Id)
                        NoteOB.NS_NoteText = TVw_Content.text;
            
            [self.delegate CancelPressed_EditNotes];
        }
        
        
    }
}

#pragma mark - UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.frame = CGRectMake(self.frame.origin.x,  ((DetailViewController *)self.delegate).view.bounds.size.height-self.frame.size.height-KEYBOARDHEIGHT, self.frame.size.width, self.frame.size.height);
    [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.center = CGPointMake(((DetailViewController *)self.delegate).view.bounds.size.width/2, ((DetailViewController *)self.delegate).view.bounds.size.height/2);
    [UIView commitAnimations];
}

@end
